﻿// using Microsoft.AspNetCore.Identity;

namespace EasyDDD.AdminModule.Application.CommandsSysAdmin
{
    public record SysAdminResetPasswordCommand : ICommand<string>
    {
        public string UserName { get; set; } = default!;
        public string Password { get; set; } = default!;
    }

    public class SysAdminResetPasswordCommandHandler(
        //IMapper mapper,
        //IConfiguration configuration,
        Repository.SysAdminRepository repository,
        //UserManager<Domain.SysAdminAggregate.ApplicationUser> userManager,
        //SignInManager<Domain.SysAdminAggregate.ApplicationUser> signInManager,
        //JwtBearerHandler jwtBearerHandler,
        ILogger<SysAdminCreateCommandHandler> logger)
        : ICommandHandler<SysAdminResetPasswordCommand, string>
    {
        public async Task<string> Handle(SysAdminResetPasswordCommand request, CancellationToken cancellationToken)
        {
            logger.LogDebug("命令 SysAdminResetPasswordCommandHandler");

            var entity = await repository.AsQueryable().FirstAsync(e => e.UserName == request.UserName || e.PhoneNumber == request.UserName || e.Email == request.UserName);
            //var token = await userManager.GeneratePasswordResetTokenAsync(entity);
            //var result = await userManager.ResetPasswordAsync(entity, token, request.Password);
            //if (!result.Succeeded)
            //{
            //    throw new BusinessException("重置密码失败") { Errors = result.Errors.ToList<object>() };
            //}
            //var passwordHash = new PasswordHasher().HashPassword(request.Password);
            //entity.SetPassword(passwordHash);

            entity.SetPassword(request.Password);

            return "重置密码成功";
        }
    }
}